﻿using Windows_AI_Assistant.Helpers;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;

namespace Windows_AI_Assistant.ViewModels.Pages
{
    public partial class SettingsViewModel : ObservableObject, INavigationAware
    {
        private bool _isInitialized = false;

        // ini文件路径
        private static readonly string IniPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.ini");

        private string _themeColor = String.Empty;

        // ini文件操作类
        private readonly IniFiles Ini = new(IniPath);

        [ObservableProperty]
        private string _appVersion = String.Empty;

        [ObservableProperty]
        private ApplicationTheme _currentTheme = ApplicationTheme.Unknown;

        public void OnNavigatedTo()
        {
            if (!_isInitialized)
                InitializeViewModel();
                InitializeTheme();
        }

        public void OnNavigatedFrom() { }

        private void InitializeViewModel()
        {
            CurrentTheme = ApplicationThemeManager.GetAppTheme();
            AppVersion = $"Windows AI Assistant - {GetAssemblyVersion()}";

            _isInitialized = true;
        }

        private void InitializeTheme()
        {
            _themeColor = Ini.IniReadValue("ThemeSetting", "THEME_COLOR");
            if (string.IsNullOrEmpty(_themeColor) || _themeColor == "1")
            {
                ApplicationThemeManager.Apply(ApplicationTheme.Light);
                CurrentTheme = ApplicationTheme.Light;
            }
            else if (_themeColor == "2")
            {
                ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                CurrentTheme = ApplicationTheme.Dark;
            } else
            {
                ApplicationThemeManager.Apply(ApplicationTheme.Unknown);
                CurrentTheme = ApplicationTheme.Unknown;
            }
        }

        private void SaveTheme()
        {
            Ini.IniWriteValue("ThemeSetting", "THEME_COLOR", _themeColor);
        }

        private string GetAssemblyVersion()
        {
            return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString()
                ?? String.Empty;
        }

        [RelayCommand]
        private void OnChangeTheme(string parameter)
        {
            switch (parameter)
            {
                case "theme_light":
                    _themeColor = "1";
                    SaveTheme();
                    if (CurrentTheme == ApplicationTheme.Light)
                        break;

                    ApplicationThemeManager.Apply(ApplicationTheme.Light);
                    CurrentTheme = ApplicationTheme.Light;

                    break;

                case "theme_dark":
                    _themeColor = "2";
                    SaveTheme();
                    if (CurrentTheme == ApplicationTheme.Dark)
                        break;

                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    CurrentTheme = ApplicationTheme.Dark;

                    break;

                case "theme_system":
                    _themeColor = "0";
                    SaveTheme();
                    if (CurrentTheme == ApplicationThemeManager.GetAppTheme())
                        break;

                    ApplicationThemeManager.Apply(ApplicationThemeManager.GetAppTheme());
                    CurrentTheme = ApplicationThemeManager.GetAppTheme();

                    break;

                default:
                    if (CurrentTheme == ApplicationTheme.Dark)
                        break;

                    ApplicationThemeManager.Apply(ApplicationTheme.Dark);
                    CurrentTheme = ApplicationTheme.Dark;

                    break;
            }
        }
    }
}
